package com.xxxx.manager.service.impl;

import com.xxxx.common.utils.CookieUtil;
import com.xxxx.manager.service.CookieService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * CookieService实现类
 *
 * @Author GP
 * @Date 2020/8/31 19:43
 */
@Service
public class CookieServiceImpl implements CookieService {
    /**
     * 将ticket存入Cookie中，
     * 1、判断ticket是否存在
     * 2、使用工具类，将ticket存入cookie中
     *
     * @return
     */
    @Override
    public Boolean setCookie(HttpServletRequest request, HttpServletResponse response, String ticket) {
        //设置Cookie
        try {
            CookieUtil.setCookie(request, response, "userTicket", ticket);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 从浏览器中获取cookie，使用工具类
     */
    @Override
    public String getCookie(HttpServletRequest request) {
        return CookieUtil.getCookieValue(request,"userTicket");
    }

    /**
     * 从浏览器中删除cookie,使用工具类
     */
    @Override
    public Boolean deleteCookie(HttpServletRequest request, HttpServletResponse response) {

        //抓取错误，无异常返回true，有异常返回false
        try {
            CookieUtil.deleteCookie(request,response,"userTicket");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
